﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace PSSMS.View.SysManage
{
    /// <summary>
    /// SMerchandise.xaml 的交互逻辑
    /// </summary>
    public partial class SMerchandise : UserControl
    {
        BLL.SMerchandise.SMerchandiseClient myClient = new BLL.SMerchandise.SMerchandiseClient();
        string strContent = string.Empty;//条件

        //实例化表格：接收筛选数据
        DataTable dtNew = new DataTable();
        public SMerchandise()
        {
            InitializeComponent();
        }
      
        /// <summary>
        /// 用户控件加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SMerchandise_Loaded(object sender, RoutedEventArgs e)
        {
            //查询商品管理信息
            SMerchan();
            //控制修改和删除按钮禁用
            btnReset.IsEnabled = false;//禁用修改按钮
            btnReset.Opacity = 0.5;
            btnSave.IsEnabled = false;//禁用删除按钮
            btnSave.Opacity = 0.5;
        }
        private void SMerchan()
        {
            //调用服务查询方法
            dtNew = myClient.CheckSMerchandise().Tables[0];
            DgrdModel.ItemsSource = dtNew.DefaultView;
        }
        /// <summary>
        /// 条件筛选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SMerch_SelectionChanged(object sender, RoutedEventArgs e)
        {
            //获取全部数据
            //strContent = SMerchan.Text.Trim();//获取控件值
            SMerchan();//刷新数据
        }

        /// <summary>
        /// 选中行改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DgrdModel_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            //判断是否选中数据
            if (DgrdModel.SelectedItem != null)
            {
                //操作按钮
                btnReset.IsEnabled = true;//启用修改按钮
                btnReset.Opacity = 1;
                btnSave.IsEnabled = true;//启用删除按钮
                btnSave.Opacity = 1;
            }
        }

        /// <summary>
        /// 商品增加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnAll_Click(object sender, RoutedEventArgs e)
        {
            tjAppend tj = new tjAppend();
            tj.Show();
            SMerchan();//刷新数据
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //1、判断是否选中表格数据
                if (DgrdModel.SelectedItem != null)
                {
                    //2、获取表格选中行
                    DataRowView data = (DataRowView)DgrdModel.SelectedItem;
                    //3、实例化修改窗口创建对象
                    tjAppend tj = new tjAppend();
                    tj.ShowDialog();//打开窗口
                    SMerchan();//刷新数据
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //1、判断是否选中表格数据
                if (DgrdModel.SelectedItem != null)
                {
                    //2、获取选中行
                    DataRowView rowView = (DataRowView)DgrdModel.SelectedItem;
                    //3、提取单元格获取主键
                    int intsupplierID = Convert.ToInt32(rowView.Row["supplierID"]);
                    //4、调用方法执行删除
                    //int intCount = myClient.DelectStaff(intsupplierID);
                    //if (intCount > 0)
                    //{
                    //    //5、提示
                    //    MessageBox.Show("删除成功!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Information);
                    //    //6、刷新
                    //    SMerchan();//刷新数据
                    //}
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);
            }
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Excel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //判断表格是否有数据
                if (DgrdModel.Items.Count > 0)
                {
                    //调用导出方法
                    Tools.Utils.ExportToExcel.Export(DgrdModel, "商品信息");
                }
                else
                {
                    MessageBox.Show("表格无数据！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Stop);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);
            }
        }

        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnImport_Click(object sender, RoutedEventArgs e)
        {

        }
      
    }
}
